//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class AggregateGalleryListing : GalleryBaseTemplatedWebControl
	{

		#region Child Controls

		private Repeater GroupRepeater;

		#endregion

		#region Public Properties

		[DefaultValue( 10 )]
		public virtual Int32 ItemsPerPage
		{
			get
			{
				Object state = ViewState["ItemsPerPage"];
				if(state != null)
					return (Int32)state;
				return 10;
			}
			set
			{ ViewState["ItemsPerPage"] = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			GroupRepeater = (Repeater)FindControl( "GroupRepeater" );
			GroupRepeater.ItemCreated += new RepeaterItemEventHandler(GroupRepeater_ItemCreated);
			GroupRepeater.ItemDataBound += new RepeaterItemEventHandler(GroupRepeater_ItemDataBound);

            Head.AddSiteNameTitle("Photo Galleries",Context);
            UsersOnline.SetLocation("Photo Galleries");

		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(EventArgs e)
		{
			if(!Page.IsPostBack)
				this.DataBind();
			base.OnLoad(e);
		}

		public override void DataBind()
		{
			base.DataBind ();
			BindData();
		}

		#endregion

		private void BindData()
		{
			GroupRepeater.DataSource = Galleries.GetGalleryGroups(true, false, false);
			GroupRepeater.DataBind();
		}

		private void GroupRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			Group group = e.Item.DataItem as Group;

			if(group == null)
				return;

			if (e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem)
			{
				GalleryRepeater galleryRepeater = e.Item.FindControl( "GalleryRepeater" ) as GalleryRepeater;

				galleryRepeater.Mode = ControlUserMode.GalleryUser;
				galleryRepeater.Paginate = true;
				galleryRepeater.ItemsPerPage = ItemsPerPage;
				
				if(Page.Request.QueryString[group.GroupID.ToString() + "page"] != null)
					galleryRepeater.CurrentPage = int.Parse(Page.Request.QueryString[group.GroupID.ToString() + "page"]);
				
				galleryRepeater.DataBinding += new EventHandler(galleryRepeater_DataBinding);
				galleryRepeater.ItemDataBound += new RepeaterItemEventHandler(galleryRepeater_ItemDataBound);
			}
		}

		private void galleryRepeater_DataBinding(object sender, EventArgs e)
		{
			SectionRepeater SectionList = sender as GalleryRepeater;
			RepeaterItem container = (RepeaterItem)SectionList.NamingContainer;
			Group group = container.DataItem as Group;
			SectionList.GroupID = group.GroupID;
		}

		private void GroupRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Group group = e.Item.DataItem as Group;
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					Literal groupTitle = (Literal)e.Item.FindControl( "GroupTitle" );
					if(groupTitle != null)
						groupTitle.Text = group.Name;
					break;
			}
		}

		private void galleryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Gallery gallery = e.Item.DataItem as Gallery;
			
			if(gallery==null)
				return;

			if(!gallery.IsActive)
				return;
			
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Header:
					GalleryRepeater repeater = sender as GalleryRepeater;
					PagingControl pagination = (PagingControl)e.Item.FindControl("Pagination");
					if((pagination != null) && (repeater != null))
					{
						// Setup the pagination
						pagination.CurrentPage = repeater.CurrentPage;
						pagination.TotalPages = (int)Math.Ceiling( (double)repeater.TotalGalleries / (double)ItemsPerPage );
						pagination.PrefixText = "page";
						pagination.QueryName = repeater.GroupID.ToString() + "page";
						pagination.NavigateUrl = Page.Request.RawUrl;
						pagination.DataBind();
					}
					break;
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					GalleryImage randomPicture = (GalleryImage)e.Item.FindControl( "RandomPicture" );
					HyperLink name = (HyperLink)e.Item.FindControl( "Name" );
					Literal description = (Literal)e.Item.FindControl( "Description" );
					Literal dateCreated = (Literal)e.Item.FindControl( "DateCreated" );
					Literal dateChanged = (Literal)e.Item.FindControl( "DateChanged" );
					Literal totalPictures = (Literal)e.Item.FindControl( "TotalPictures" );

					if(randomPicture != null)
					{
						GalleryPost galleryPost = GalleryPosts.GetRandomPicture( gallery.SectionID, -1 );
						if(galleryPost != null)
						{
							randomPicture.PostID = galleryPost.PostID;
							randomPicture.NavigateUrl = GalleryUrls.Instance().ViewGallery(gallery.ApplicationKey);
							randomPicture.GalleryPost = galleryPost;
						}
						else
							randomPicture.Visible = false;
					}

					if(name != null)
					{
						name.Text = gallery.Name;
						name.NavigateUrl = GalleryUrls.Instance().ViewGallery( gallery.ApplicationKey );
					}

					if (description != null)
					{
						if(gallery.Description.Length > 50)
							description.Text = gallery.Description.Substring(0, 50) + "...";
						else if (description != null)
							description.Text = gallery.Description;

						if(description.Text != string.Empty)
							description.Text += "<br/>";
					}

					if (totalPictures != null)
						totalPictures.Text = gallery.TotalThreads.ToString();

					if(gallery.MostRecentPostDate == SqlDateTime.MinValue.Value.AddYears(44) && dateChanged != null)
						dateChanged.Visible = false;
					else
					{
						User pUser = Users.GetUser(gallery.MostRecentPostAuthorID, false);
						DateTime changedDate = gallery.MostRecentPostDate;
						DateTime createdDate = gallery.DateCreated;

						if(!pUser.IsAnonymous)
						{
							changedDate = pUser.GetTimezone(gallery.MostRecentPostDate);
							createdDate = pUser.GetTimezone(gallery.DateCreated);
						}

//						User cUser = CSContext.Current.User;
//						string dateFormat;
//						if(!cUser.IsAnonymous)
//						{
//							dateFormat = cUser.Profile.DateFormat;
//						}
//						else
//							dateFormat = CSContext.Current.SiteSettings.DateFormat;

						if(dateChanged != null)
							dateChanged.Text = Formatter.FormatAgoDate(changedDate);//.ToString(dateFormat) + " " + changedDate.ToString(CSContext.Current.SiteSettings.TimeFormat);

						if(dateCreated != null)
							dateCreated.Text = Formatter.FormatAgoDate(createdDate);//createdDate.ToString(dateFormat) + " " + createdDate.ToString(CSContext.Current.SiteSettings.TimeFormat);
					}
					break;
			}
		}
	}
}
